package com.yw.atp.model;

import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.yw.atp.Constant;
import com.yw.atp.util.DateUtil;

@Entity
@Table(name=Constant.DB_PREFIX+"command")
public class Command implements IDEntity{
	private static final long serialVersionUID = 1L;

	@Id
    @GeneratedValue
    private Integer id;
	@Column(length=20000)
	private String content;
	@ManyToOne
	private Staff issuer;
	@ManyToOne
	private Staff sender;
	@Temporal(TemporalType.TIMESTAMP)
	private Date issueTime;
	@Temporal(TemporalType.TIMESTAMP)
	private Date sendTime;
	@Column(columnDefinition="varchar(10) default '0'")
	private String sendType;
	private boolean top = false;
	private String reciveType;
	@ManyToMany
	@JoinTable(name=Constant.DB_PREFIX+"depart_command",
			joinColumns=@JoinColumn(name="command_id",referencedColumnName="id"),
			inverseJoinColumns=@JoinColumn(name="depart_id",referencedColumnName="id"))
	private Set<Department> deparList;
	@ManyToMany
	@JoinTable(name=Constant.DB_PREFIX+"profe_command",
			joinColumns=@JoinColumn(name="command_id",referencedColumnName="id"),
			inverseJoinColumns=@JoinColumn(name="pro_id",referencedColumnName="id"))
	private Set<Profession> proList;
	@ManyToMany
	@JoinTable(name=Constant.DB_PREFIX+"staff_command",
			joinColumns=@JoinColumn(name="command_id",referencedColumnName="id"),
			inverseJoinColumns=@JoinColumn(name="staff_id",referencedColumnName="id"))
	private Set<Staff> staffList;
	@Temporal(TemporalType.TIMESTAMP)
	@OrderBy("createdAt desc")
	private Date createdAt;
	@OneToMany(mappedBy="command",fetch=FetchType.LAZY,cascade=CascadeType.PERSIST)
    private Set<Reply> replyList;
	@ManyToOne
	private AtpImage atpImage;
	private String commType;
    private String latitude;
    private String longitude;
    private String title;
    private String status = "0";
	private Date updateAt;
	private String replyStatus;
	public String getReplyStatus() {
		return replyStatus;
	}

	public void setReplyStatus(String replyStatus) {
		this.replyStatus = replyStatus;
	}

	public Date getUpdateAt() {
		return updateAt;
	}

	public void setUpdateAt(Date updateAt) {
		this.updateAt = updateAt;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getLatitude() {
		return latitude;
	}

	public void setLatitude(String latitude) {
		this.latitude = latitude;
	}

	public String getLongitude() {
		return longitude;
	}

	public void setLongitude(String longitude) {
		this.longitude = longitude;
	}

	public String getCommType() {
		return commType;
	}

	public void setCommType(String commType) {
		this.commType = commType;
	}

	public AtpImage getAtpImage() {
		return atpImage;
	}

	public void setAtpImage(AtpImage atpImage) {
		this.atpImage = atpImage;
	}

	public Set<Reply> getReplyList() {
		return replyList;
	}

	public void setReplyList(Set<Reply> replyList) {
		this.replyList = replyList;
	}

	public Date getCreatedAt() {
		return createdAt;
	}

	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}

	public String getReciveType() {
		return reciveType;
	}

	public void setReciveType(String reciveType) {
		this.reciveType = reciveType;
	}

	public Set<Department> getDeparList() {
		return deparList;
	}

	public void setDeparList(Set<Department> deparList) {
		this.deparList = deparList;
	}

	public Set<Profession> getProList() {
		return proList;
	}

	public void setProList(Set<Profession> proList) {
		this.proList = proList;
	}

	public Set<Staff> getStaffList() {
		return staffList;
	}

	public void setStaffList(Set<Staff> staffList) {
		this.staffList = staffList;
	}

	public boolean isTop() {
		return top;
	}

	public void setTop(boolean top) {
		this.top = top;
	}

	public String getSendType() {
		return sendType;
	}

	public void setSendType(String sendType) {
		this.sendType = sendType;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Staff getIssuer() {
		return issuer;
	}

	public void setIssuer(Staff issuer) {
		this.issuer = issuer;
	}

	public Staff getSender() {
		return sender;
	}

	public void setSender(Staff sender) {
		this.sender = sender;
	}
    
	@Temporal(TemporalType.TIMESTAMP)
	public Date getIssueTime() {
		return issueTime;
	}
    
	@Temporal(TemporalType.TIMESTAMP)
	public void setIssueTime(Date issueTime) {
		this.issueTime = issueTime;
	}
    
	@Temporal(TemporalType.TIMESTAMP)
	public Date getSendTime() {
		return sendTime;
	}
    
	@Temporal(TemporalType.TIMESTAMP)
	public void setSendTime(Date sendTime) {
		this.sendTime = sendTime;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
    
	public static JSONArray capture(List<Command> appms) throws JSONException{
		JSONArray jsonAppms = new JSONArray();
		for(Command appm : appms){
			jsonAppms.put(appm.capture());
		}
		return jsonAppms;
	}
	
	public JSONObject capture() throws JSONException{
		JSONObject appm = new JSONObject();
		appm.put("id",id);
		if(content==null||"".equals(content)){
			appm.put("content","这家伙很懒,什么也没留下.");
		}else{
			appm.put("content",content);
		}
		
		appm.put("sendType",sendType);
		appm.put("top",top);
		
		if(issuer!=null){
			appm.put("issuer",issuer.capture());
		}else{
			appm.put("issuer","{}");
		}
		if(sender!=null){
			appm.put("sender",sender.capture());
		}else{
			appm.put("sender","{}");
		}
		if(atpImage!=null&&atpImage.getPath()!=null){
			appm.put("imgSrc",atpImage.getPath());
		}else{
			appm.put("imgSrc","");
		}
		
		appm.put("createdAt",createdAt!=null?DateUtil.getClientFormatA(createdAt):" ");
		
		if("2".equals(commType)){
			appm.put("sendTime",sendTime!=null?DateUtil.getServiceFormat(sendTime):"");
			appm.put("issueTime",issueTime!=null?DateUtil.getServiceFormat(issueTime):"");
			appm.put("reciveType",reciveType);
			appm.put("commType",commType);
			appm.put("latitude",latitude);
			appm.put("longitude",longitude);
			appm.put("title",title);
			appm.put("status",status);
			if(deparList!=null&&deparList.size()>0){
				appm.put("deparList",Department.capture(deparList));
			}else{
				appm.put("deparList",new JSONArray());
			}
			if(proList!=null&&proList.size()>0){
				appm.put("proList",Profession.capture(proList));
			}else{
				appm.put("proList",new JSONArray());
			}
			if(staffList!=null&&staffList.size()>0){
				appm.put("staffList",Staff.capture(staffList));
			}else{
				appm.put("staffList",new JSONArray());
			}
		}else{
			appm.put("replyStatus",replyStatus);
		}
		return appm;
	}
}
